package info.openlvb.openface.client.base.person;

import info.openlvb.openface.client.base.Account;
import info.openlvb.openface.client.handler.DeleteGroupListener;
import info.openlvb.openface.client.handler.DeleteUserInGroupListener;
import info.openlvb.openface.client.handler.ModifyGroupListener;
import info.openlvb.openface.client.handler.addMemberInGroupListener;
import info.openlvb.openface.client.service.LdapService;
import info.openlvb.openface.client.service.LdapServiceAsync;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

public class MemberInGroupWindow extends DialogBox{

	//private Record record;
	private TextBox txt_nom, txt_desc;
	FlexTable table;
	//private ListGroupPanel listGroupPanel;
	MultiWordSuggestOracle oracle;
	private final SuggestBox sb;
	private final LdapServiceAsync ldapService = GWT.create(LdapService.class);

	public MemberInGroupWindow(Record record, final ListGroupPanel listGroupPanel){
		super();
		//this.record = record;
		//this.listGroupPanel = listGroupPanel;
		setTitle("Gestion d'un groupe");
		setAnimationEnabled(true);
		setGlassEnabled(true);
		VerticalPanel content = new VerticalPanel();
		HTML lbl_title = new HTML("<h3>Modification d'un groupe</h3></br>");
		content.add(lbl_title);

		FlexTable descTable = new FlexTable();
		descTable.setCellSpacing(20);
		Label lbl_nom = new Label("Nom du groupe");
		descTable.setWidget(0, 0, lbl_nom);
		txt_nom = new TextBox();
		txt_nom.setText(record.getAsString("nom"));
		descTable.setWidget(0, 1, txt_nom);

		Label lbl_desc = new Label("Description du groupe");
		descTable.setWidget(1, 0, lbl_desc);
		txt_desc = new TextBox();
		txt_desc.setText(record.getAsString("description"));
		descTable.setWidget(1, 1, txt_desc);

		oracle = new MultiWordSuggestOracle();
		sb = new SuggestBox(oracle);
		ldapService.searchUsersUid(new AsyncCallback<List<String>>() {
			public void onSuccess(List<String> result) {
				Iterator<?> i = result.iterator();
				while(i.hasNext()){
					oracle.add((String) i.next());
				}
			}
			public void onFailure(Throwable caught) {
				MessageBox.alert("probleme", "probleme de communication avec le backend");
			}
		});
		Label lbl_membres =new Label("Membres");
		descTable.setWidget(2, 0, lbl_membres);
		Button addMember = new Button("Ajouter comme membre");
		descTable.setWidget(3, 0, sb);
		descTable.setWidget(3, 1, addMember);
		final addMemberInGroupListener listener = new addMemberInGroupListener(MemberInGroupWindow.this,record.getAsString("nom"));
		addMember.addListener(listener);
		sb.addSelectionHandler(new SelectionHandler<Suggestion>() {
			public void onSelection(SelectionEvent<Suggestion> event) {
				listener.setNewuser(sb.getText());
			}
		});
		content.add(descTable);
		table = new FlexTable();
		table.setTitle("Liste des membres");
		table.addStyleName("table");

		ldapService.searchMembersByCnOfGroup(record.getAsString("nom"), new AsyncCallback<List<Account>>() {
			public void onFailure(Throwable caught) {
				MessageBox.alert("probleme", "probleme de communication avec le backend");
			}
			public void onSuccess(List<Account> result) {
				if(result != null){
					//for (Iterator<?> iterator = result.iterator();iterator.hasNext();) {
					Iterator<?> iterator = result.iterator();
					int i=0;
					while(iterator.hasNext()){

						Account account_cour = (Account) iterator.next();
						table.setWidget(i, 0, new Label(account_cour.getLogin()));//menuFactory(i,account_cour.getGroupe_courant(),account_cour.getLogin()));
						table.setWidget(i, 1, new Label(account_cour.getNom()));
						table.setWidget(i, 2, new Label(account_cour.getPrenom()));
						Button button = new Button("Remove");
						button.setIcon("images/cancel.png");
						DeleteUserInGroupListener deleteListener = new DeleteUserInGroupListener(MemberInGroupWindow.this,account_cour.getGroupe_courant(),account_cour.getLogin());
						deleteListener.setRow(i);
						button.addListener(deleteListener);
						
						table.setWidget(i, 3,button);
						i++;
					}
				}
			}
		});

		content.add(table);
		Button closeBtn = new Button("Close");
		closeBtn.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				MemberInGroupWindow.this.hide();
			}			
		});
		
		Button deleteBtn = new Button("Delete this group");
		deleteBtn.addListener(new DeleteGroupListener(this,listGroupPanel,record.getAsString("nom")));
		
		Button modifyBtn = new Button("Modify this group");
		modifyBtn.addListener(new ModifyGroupListener(this,listGroupPanel,record.getAsString("nom")));
		
		Panel btPanel = new Panel();
		btPanel.addButton(modifyBtn);
		btPanel.addButton(deleteBtn);
		btPanel.addButton(closeBtn);
		content.add(btPanel);
		add(content);
		show();
	}
	public FlexTable getUserTable(){
		return table;
	}
	public SuggestBox getSb(){
		return sb;
	}
	public TextBox getTxt_nom() {
		return txt_nom;
	}
	public TextBox getTxt_desc() {
		return txt_desc;
	}
	

}
